#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.db import models
from django.utils.timezone import localtime
from devices.models import Device

class Maintain(models.Model):
    '''维修记录，对应flag编号2'''
    malfunction = models.CharField(verbose_name='故障简述', max_length=100)
    solution = models.CharField(verbose_name='解决方案', max_length=200)
    date = models.DateTimeField(verbose_name='出现时间', auto_now_add=True)
    head = models.CharField(verbose_name='负责人', max_length=10)
    repairman = models.CharField(verbose_name='维修人', max_length=10)
    device = models.ForeignKey(Device, verbose_name='维修设备')
    def __unicode__(self):
        '''返回信息 TODO:此处数据库引用外键出现编码错误'''
        info = localtime(self.date).strftime('%Y-%m-%d %H:%M:%S')
        return info
    def phone(self):
        '''对手机返回，特定外键字段的(verbose_name:字段值)组成的字典'''
        info = {}
        dic = {
            'malfunction':self.malfunction,
            'solution':self.solution,
            'date':self.date,
        }
        li = dic.keys()
        for i in li:
            if i == 'date':
                info[i] = localtime(self.date).strftime('%Y-%m-%d %H:%M:%S')
            else:
                info[i] = self.__dict__[i]
        return info
    class Meta:
        verbose_name = '维修记录'
        verbose_name_plural = '维修记录列表'
